<style>
form input.qty{width:50px;}
</style>
<style>
	tr.sub_row{background:#CCC;}
	tr.sub_row td{padding-left:20px;}
        .noLBorder td{border-left: 0px none; padding: 5px;}
        .nopad{padding:0px;text-align: center;}
        .textbox_quantity input{
           width:50px!important;
           padding:1px!important;
        }
        .box .textbox_quantity input.textbox_readonly:focus{
            border: none;
           -moz-box-shadow:none;
        }
        .textbox_readonly, .permanent_readonly{
            background: none;
            border: 0px;
        }
        .left{
            float: left;
}
.clear{
    clear:both;
}
</style>
<div class="componentheading"><?php echo $page_title;?></div>
<div><?php global_message(); ?></div>
<?php
	$isEdit = isset($publication) ? true : false;
	$text = empty($isEdit) ? 'Add' : 'Update';
	$selected = 'selected = "selected"';
?>
<form method="post" id="newForm" enctype="multipart/form-data">
<div class="box-header toggleNext">Publication :: General Information</div>
<div class="box table toggleBorder">
<table width="100%">
	<tr>
    	<td width="22%"><label class="required">Title</label></td>
        <td colspan="3">
        	<input class="required width400" type="text" name="title" id="title" value="<?php if($isEdit) echo $publication->title;?>" />
        </td>
    </tr>
	<tr>
    	<td><label>Thematic Area</label></td>
        <td colspan="3">
            <select name="category_id" id="category_id" class="required">
            	<?php foreach($categories as $category) :?>
                	<option value="<?php echo $category->id;?>" <?php if($isEdit){ if($publication->category_id == $category->id) echo $selected;}?>><?php echo $category->category_name;?></option>
                <?php endforeach; ?>
        	</select>
        </td>	
    </tr>
    <tr>
    	<td><label>Sub Thematic Area</label></td>
        <td colspan="3">
        	<div id="sub_category">
            <?php if($isEdit) : ?>
				<?php echo sub_category($publication->category_id, $publication->sub_category_id);?>            
            <?php else : ?>
	        	<?php echo sub_category($categories[0]->id);?>            
            <?php endif; ?>
            </div>
        </td>
    </tr>
    <tr>
    	<td><label>Code</label></td>
        <td colspan="3"><input type="text" readonly name="coding" id="disabled_code" class="readonly" value="<?php if($isEdit) echo $publication->coding;?>" /></td>
    </tr>
    <tr>
    	<td><label>Soft Copy Type</label></td>
        <td>
        <?php
            $upload_checked=$url_checked="";
            if($isEdit){
                if($publication->soft_copy_type=="url") $url_checked="checked";
                else $upload_checked="checked";
            }else{
                $upload_checked="checked";
            }
        ?>
            <label>Upload</label><input type="radio" <?php echo $upload_checked;?> value ="upload" name="soft_copy_type" onchange="changeSoftCopyType(this.value);">
            
        </td>
        <td colspan="2">
            <label>External URL</label><input type="radio" <?php echo $url_checked;?> value ="url" name="soft_copy_type" onchange="changeSoftCopyType(this.value);" >
        </td>
    </tr>
    <tr id="soft_copy_upload">
    	<td><label for="soft_copy">Soft Copy Upload</label></td>
        <td colspan="3">
             <input type="hidden" name="prev_soft_copy" id="prev_soft_copy" value="<?php if($isEdit) echo $publication->soft_copy;?>" />
            <input type="file" name="soft_copy" id="soft_copy" accept="pdf|doc|docx"/>
              <div>
                  <span class="small">(Note:- Extension: pdf | doc | docx; Max size: 102400 Kilobytes)</span>
                 <?php if(@$publication->soft_copy!="" && $isEdit && @$publication->soft_copy_type=="upload"){?>
                       <a href="<?php echo base_url().'uploads/softcopy/'.$publication->soft_copy;?>">Download soft copy</a>
                 <?php }?>
              </div>
        </td>
    </tr>
    <tr id="soft_copy_url">
    	<td><label for="soft_copy">Soft Copy External Link</label></td>
        <td colspan="3">
            <input type="text" class="width400" name="soft_copy" id="soft_copy_url" value="<?php if($isEdit) echo $publication->soft_copy;?>" />
        </td>
    </tr>
    <tr>
    	<td><label>Image</label></td>
        <td colspan="3">
        	<input type="file" name="image" id="image"  accept="gif|jpg|png"/>
            <input type="hidden" name="prev_image" id="prev_image" value="<?php if($isEdit) echo $publication->image;?>" />
            <div>
                 <span class="small">(Note:- Extension: gif | jpg | png; Max dimension: 2560 X 1920px; Max size: 10240 Kilobytes)</span>
                 <?php if($isEdit && @$publication->image!=""):?><a href="<?php echo get_image(config_item('publication_thumbs_path'), $publication->image);?>" target="_blank">Preview</a><?php endif; ?>
            </div>
        </td>
    </tr>
    <tr>
    	<td><label>Country</label></td>
        <td colspan="3"><input type="text" class="" name="country" id="country" value="<?php if($isEdit) echo $publication->country;?>" /></td>
    </tr>
    <tr>
    	<td><label>Publisher</label></td>
        <td colspan="3"><input type="text" class="width400" name="publisher" id="publisher" value="<?php if($isEdit) echo $publication->publisher;?>" /></td>
    </tr>
    <tr>
    	<td><label>No of Pages</label></td>
        <td colspan="3"><input type="text" name="pages" id="pages" value="<?php if($isEdit) echo $publication->pages;?>" /></td>
    </tr>
    <tr>
    	<td><label>Year of Publication</label></td>
        <td colspan="3">
            <select name="publish_year" id="publish_year">
                <option>Select Year</option>
                <?php for($i=1900;$i<=2050;$i++){
                    $selected_year = ($isEdit && $publication->publish_year==$i)? "selected":"";
                    ?>
                <option <?php echo $selected_year;?> value="<?php echo $i;?>"><?php echo $i;?></option>
                <?php }?>
            </select>
        </td>
    </tr>
    <tr>
    	<td><label>ISBN Number</label></td>
        <td colspan="3"><input type="text" name="isbn_number" id="isbn_number" value="<?php if($isEdit) echo $publication->isbn_number;?>" /></td>
    </tr>
</table>
</div>
<div class="box-header toggleNext">Publication :: Manage Quantities</div>
<div class="box table toggleBorder">
    <?php
   if($isEdit){
       
        $publication_quantity_id = $publication->id;
        $CI = &get_instance();
        $CI->load->model('quantity_model');
        $publication_en = $CI->quantity_model->get_quantity_by_pub_type($publication_quantity_id, 'en');
        $publication_np = $CI->quantity_model->get_quantity_by_pub_type($publication_quantity_id, 'np');
        //debug_array($publication_en);exit;
   }else{
       
      $publication_quantity_id="";
   }
    ?>
      <table width="100%">
        <thead>
          <tr>
                <th class="nopad" width="20%" style="border-left:0px;"></th>
                <th class="nopad" width="20%">Quantity for distribution</th>
                <th class="nopad" width="20%">Quantity in box</th>
                <th class="nopad" width="20%">Quantity in store</th>
                <th class="nopad" width="20%">Grand total</th>
          </tr>
       </thead>
       <tbody>
                    <tr id="textbox_readonly-<?php echo $publication_quantity_id;?>" class="textbox_quantity">
                        <td class="nopad" colspan="5">
                            <table width="100%" id="tbl_quantity_<?php echo $publication_quantity_id;?>">
                                <tr>
                        
                        <td class="nopad" width="20%">
                              <table cellspacing="0" cellpadding="0" class="noLBorder">
                                <tr>
                                    <td>English:</td>
                                </tr>
                                <tr>
                                    <td>Nepali:</td>
                                </tr>
                                <tr>
                                    <td>Total:</td>
                                </tr>
                                </table>
                        </td>
                        <td class="nopad" width="20%">
                            <table cellspacing="0" cellpadding="0" class="noLBorder">
                                <tr>
                                    <td><input type="text" onkeydown="return suppressNonNumericInput(event)" class="calc_quantity" name="qty_distribution_en" id="total_qty_distribution_en_<?php echo $publication_quantity_id;?>" value="<?php echo ($isEdit)? $publication_en->total_qty_distribution:0;?>"></td>
                                </tr>
                                <tr>
                                    <td><input type="text" onkeydown="return suppressNonNumericInput(event)" class=" calc_quantity" name="qty_distribution_np" id="total_qty_distribution_np_<?php echo $publication_quantity_id;?>" value="<?php echo ($isEdit)? $publication_np->total_qty_distribution:0;?>"></td>
                                </tr>
                                <tr>
                                    <td><input readonly type="text" class="textbox_readonly permanent_readonly" name="total_qty_distribution_total_<?php echo $publication_quantity_id;?>" id="total_qty_distribution_total_<?php echo $publication_quantity_id;?>" value="<?php echo ($isEdit)? $publication_en->total_qty_distribution+$publication_np->total_qty_distribution:0;?>"></td>
                                </tr>
                            </table>
                        </td>
                        <td class="nopad" width="20%">
                            <table cellspacing="0" cellpadding="0" class="noLBorder">
                                <tr>
                                   <td><input type="text" onkeydown="return suppressNonNumericInput(event)" class=" calc_quantity" name="qty_box_en" id="total_qty_box_en_<?php echo $publication_quantity_id;?>" value="<?php echo ($isEdit)? $publication_en->total_qty_box:0;?>"></td>
                                </tr>
                                <tr>
                                    <td><input type="text" onkeydown="return suppressNonNumericInput(event)" class=" calc_quantity" name="qty_box_np" id="total_qty_box_np_<?php echo $publication_quantity_id;?>" value="<?php echo ($isEdit)? $publication_np->total_qty_box:0;?>"></td>
                                </tr>
                                <tr>
                                    <td><input readonly type="text" class="textbox_readonly permanent_readonly" name="total_qty_box_total_<?php echo $publication_quantity_id;?>" id="total_qty_box_total_<?php echo $publication_quantity_id;?>" value="<?php echo ($isEdit)? $publication_en->total_qty_box+$publication_np->total_qty_box:0;?>"></td>
                                </tr>
                            </table>
                        </td>
                        <td class="nopad" width="20%">
                            <table cellspacing="0" cellpadding="0" class="noLBorder">
                                <tr>
                                   <td><input type="text" onkeydown="return suppressNonNumericInput(event)" class=" calc_quantity" name="qty_store_en" id="total_qty_store_en_<?php echo $publication_quantity_id;?>" value="<?php echo ($isEdit)? $publication_en->total_qty_store:0;?>"></td>
                                </tr>
                                <tr>
                                    <td><input type="text" onkeydown="return suppressNonNumericInput(event)" class=" calc_quantity" name="qty_store_np" id="total_qty_store_np_<?php echo $publication_quantity_id;?>" value="<?php echo ($isEdit)? $publication_np->total_qty_store:0;?>"></td>
                                </tr>
                                <tr>
                                    <td><input readonly type="text" class="textbox_readonly permanent_readonly" name="total_qty_store_total_<?php echo $publication_quantity_id;?>" id="total_qty_store_total_<?php echo $publication_quantity_id;?>" value="<?php echo ($isEdit)? $publication_en->total_qty_store+$publication_np->total_qty_store:0;?>"></td>
                                </tr>
                            </table>
                        </td>
                        <td class="nopad" width="20%">
                            <table cellspacing="0" cellpadding="0" class="noLBorder">
                                <tr>
                                   <td><input type="text" readonly class="textbox_readonly permanent_readonly" name="total_qty_en" id="total_qty_grand_en_<?php echo $publication_quantity_id;?>" value="<?php echo ($isEdit)? $publication_en->total_qty:0;?>"></td>
                                </tr>
                                <tr>
                                    <td><input type="text" readonly class="textbox_readonly permanent_readonly" name="total_qty_np" id="total_qty_grand_np_<?php echo $publication_quantity_id;?>" value="<?php echo ($isEdit)? $publication_np->total_qty:0;?>"></td>
                                </tr>
                                <tr>
                                    <td><input type="text" readonly class="textbox_readonly permanent_readonly" name="total_qty_grand_total_<?php echo $publication_quantity_id;?>" id="total_qty_grand_total_<?php echo $publication_quantity_id;?>" value="<?php echo ($isEdit)? $publication_en->total_qty+$publication_np->total_qty:0;?>"></td>
                                </tr>
                            </table>
                        </td>
                       </tr>
                            </table>
                        </td>
                    </tr>

        </tbody>
    </table>
</div>

<div class="box-header toggleNext">Publication :: Others</div>
<div class="box table toggleBorder">
<table width="100%">
    <tr>
    	<td width="10%" valign="top"><label>Abstract</label></td>
        <td width="90%" colspan="3"><textarea name="abstract" id="abstract" cols="50" rows="4"><?php if($isEdit) echo $publication->abstract;?></textarea></td>
    </tr>
    <tr>
    	<td valign="top"><label>Remarks</label></td>
        <td colspan="3"><textarea name="remark" id="remark" cols="50" rows="4"><?php if($isEdit) echo $publication->remark;?></textarea></td>
    </tr>
</table>
</div>
<div class="padding_10top">
    <input type="submit" name="submit_publication" id="submit_publication" value="<?php echo $text;?>">
    <?php if($isEdit) : ?>
    <input type="button" onclick="window.location.href = '<?php echo filter_url('edit');?>'" value="Back" />
    <?php else: ?>
    <input type="button" onclick="window.location.href = '<?php echo site_url('publication');?>'" value="Cancel" />
    <?php endif; ?>
</div>
</form>
<script>
$(document).ready(function(){
    <?php if($url_checked=="checked"){?>changeSoftCopyType('url');<?php }else{?>changeSoftCopyType('upload');<?php }?>
	<?php if(!$isEdit){?>

$('#newForm').validate(
    {
                    rules:
                    {
                        title:
                        {
                            remote:
                            {
                                url: "<?php echo site_url("publication/check_title");?>",
                                type: "post"
                            }
                            }
                    },
                    messages:
                    {
                        title:
                        {
                            remote: "Title already exists"
                        }
                    }
                });
        generate_code();
	<?php }else{?>
$('#newForm').validate();
<?php }?>
	$('.qty').bind({
		click: function() {
			if($(this).val() == '0')
				$(this).val('');
		},
		blur: function() {
			if($.trim($(this).val()) == '')
				$(this).val('0');
		}
		
	});	
	/*$('#quantity_en').change(function(){
		//alert('ok');
		//$('#quantity_store_en').val($(this).val());
	});*/
});
$('#category_id').change(function(){
		$('#sub_category_id').attr('disabled', 'disabled');
		$.post('<?php echo site_url('publication/ajax_sub_categories');?>', {cat_id : $(this).val()}, function(data){
                    $('#sub_category').html(data);
                     generate_code();
                    $('#sub_category_id').removeAttr('disabled');
                });
	});

   $('.calc_quantity').keyup(function() {
      var MyId = $($(this)).attr('id');
      var MyIdInfo = MyId.split('_');
      //var front = MyIdInfo[0]+"_"+MyIdInfo[1]; //unused
      var type = MyIdInfo[2]; //distribution, box, store
      var en_np =MyIdInfo[3]; //eng, nep
      var public_id =MyIdInfo[4]; //publication id
      var distribution_en = $('#total_qty_distribution_en_'+public_id).val();
      var box_en = $('#total_qty_box_en_'+public_id).val();
      var store_en = $('#total_qty_store_en_'+public_id).val();
      var distribution_np = $('#total_qty_distribution_np_'+public_id).val();
      var box_np = $('#total_qty_box_np_'+public_id).val();
      var store_np = $('#total_qty_store_np_'+public_id).val();

      if(isNaN(distribution_en) || distribution_en==""){
          distribution_en = 0;
          $('#total_qty_distribution_en_'+public_id).val(distribution_en);
      }
      if(isNaN(box_en) || box_en==""){
          box_en = 0;
          $('#total_qty_box_en_'+public_id).val(box_en);
      }
      if(isNaN(store_en) || store_en==""){
          store_en = 0;
          $('#total_qty_store_en_'+public_id).val(store_en);
      }
      if(isNaN(distribution_np) || distribution_np==""){
          distribution_np = 0;
          $('#total_qty_distribution_np_'+public_id).val(distribution_np);
      }
      if(isNaN(box_np) || box_np==""){
          box_np = 0;
          $('#total_qty_box_np_'+public_id).val(box_np);
      }
      if(isNaN(store_np) || store_np==""){
          store_np = 0;
          $('#total_qty_store_np_'+public_id).val(store_np);
      }

      var total_en = parseInt(distribution_en,10)+parseInt(box_en,10)+parseInt(store_en,10);
      //console.log(parseInt(distribution_en,10));
      var total_np = parseInt(distribution_np,10)+parseInt(box_np,10)+parseInt(store_np,10);
      var grand_total = parseInt(total_en,10)+parseInt(total_np,10);
      var distribution_total = parseInt(distribution_en,10)+parseInt(distribution_np,10);
      var box_total = parseInt(box_en,10)+parseInt(box_np,10);
      var store_total = parseInt(store_en,10)+parseInt(store_np,10);
      $('#total_qty_grand_en_'+public_id).val(total_en); //total_qty_grand_en_1
      $('#total_qty_grand_np_'+public_id).val(total_np); //total_qty_grand_np_1
      $('#total_qty_grand_total_'+public_id).val(grand_total); //total_qty_grand_total_1
      $('#total_qty_distribution_total_'+public_id).val(distribution_total); //total_qty_distribution_total_1
      $('#total_qty_box_total_'+public_id).val(box_total); //total_qty_box_total_1
      $('#total_qty_store_total_'+public_id).val(store_total); //total_qty_grand_total_1
   });
   function changeSoftCopyType(val){
       if(val=="url"){
            $("#soft_copy_upload input").attr('disabled',true);
            $("#soft_copy_url input").removeAttr('disabled');
            $("#soft_copy_upload").hide();
            $("#soft_copy_url").show();
       }else{//upload
            $("#soft_copy_url input").attr('disabled',true);
            $("#soft_copy_upload input").removeAttr('disabled');
            $("#soft_copy_url").hide();
            $("#soft_copy_upload").show();
       }
   }
    function changeCurrentState(pub_id){
       var checkMe = $("#changeCurrentState_"+pub_id).hasClass('update_quantity');
        if(checkMe){
            var form_data = $("#frm_quantity_"+pub_id).serialize();
            var loading_image = "<img src='<?php echo base_url().'/assets/images/fb_loading.gif'?>'>";
            $("#changeCurrentState_"+pub_id).html(loading_image);
            $.ajax({
               type: "POST",
               url: "<?php echo site_url('quantity/ajax_update_quantity');?>",
               data: form_data,
               success: function(msg){
                   $("#show_msg").html(msg).fadeIn(300).fadeOut(300).fadeIn(300).fadeOut(300).fadeIn(300).fadeOut(300);
                  //updateFields(pub_id);
                  $("#changeCurrentState_"+pub_id).removeClass('update_quantity');
                  $("#changeCurrentState_"+pub_id).html('Edit');
                  changePublicationQuantityState('readonly', pub_id);
               }
             });
           //ajax update of sth;

        }else{
            $("#changeCurrentState_"+pub_id).addClass('update_quantity');
            $("#changeCurrentState_"+pub_id).html($("#btn_for_publication_"+pub_id).html());
            changePublicationQuantityState('edit', pub_id);
        }
    }
    function changePublicationQuantityState(type, pub_id){
        if(type=="edit"){
            $("#textbox_readonly-"+pub_id+" input").removeClass('textbox_readonly').removeAttr('readonly');

        }else if(type=="readonly"){
            $("#textbox_readonly-"+pub_id+" input").addClass('textbox_readonly').attr('readonly','readonly');
        }else{
            return false;
        }
        $("#textbox_readonly-"+pub_id+" input.permanent_readonly").addClass('textbox_readonly').attr('readonly','readonly');
    }
  function suppressNonNumericInput(event){
    if( !(event.keyCode == 8                                // backspace
        || event.keyCode == 46                              // delete
        || (event.keyCode >= 35 && event.keyCode <= 40)     // arrow keys/home/end
        || (event.keyCode >= 48 && event.keyCode <= 57)     // numbers on keyboard
        || (event.keyCode >= 96 && event.keyCode <= 105))   // number on keypad
        ) {
            event.preventDefault();     // Prevent character input

    }
}

function generate_code(){
    cat_id = $("#category_id").val();
    sub_cat_id = $("#sub_category_id").val();
   $('#disabled_code').attr('disabled', 'disabled');
    $.post("<?php echo site_url('publication/ajax_generate_code');?>", {cat_id:cat_id, sub_cat_id:sub_cat_id },function(data) {
                 $("#disabled_code").val(data);
                 $('#disabled_code').removeAttr('disabled');

    });
        }
</script>